/**
 * 验证码
 */
$(document).ready(function() {
	setTimeout("getYzm()", 100);
	$(".weui-vcode-btn").attr("disabled", true);
	getinviteUserNameByMobile();
});

function getYzm() {
	$("#img").attr("src", ip + "platform/user/getValidateCodeImg.m?t=" + Math.random());
	$("#img").attr("onerror", 'this.src=' + "'" + ip + "platform/user/getValidateCodeImg.m?t=" + Math.random() + "'" + ';this.onerror=null');
}

function Yzm() {
	var getYzmReqbody = {
		"validateCodeImg": $("#yzm").val().trim()
	};
	$.ajax({
		type: "POST",
		url: ip + "platform/user/validateCodeImg.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: getYzmReqbody,
		success: function(data) {
			if(data.statusCode == 0) {
				$(".weui-vcode-btn").attr("disabled", false);
			} else {
				$(".weui-vcode-btn").attr("disabled", true);
				if($("#yzm").val().length != 4) {

				} else {
					$.toptip(data.resultDesc, 'error');
				}
			}
		}
	});
}
/**
 * 获取短信验证码
 */
var countdown = 300;

function setInviteCode(val) {
	if($("#mobile").val().trim() == '') {
		$.toast("手机号码不能为空!", "text");
		return false;
	}
	if($("#yzm").val().trim() == '') {
		$.toast("图片验证码不能为空!", "text");
		return false;
	}
	if(/^1(3|4|5|6|7|8|9)\d{9}$/.test($("#mobile").val().trim())) {
		number = "+86|" + $("#mobile").val().trim();
	} else {
		number = $("#mobile").val().trim();
	}
	var getInviteCodeReqbody = {
		"type": 0,
		"mobile": number
	};
	$.ajax({
		type: "POST",
		url: ip + "platform/user/getValidateCode.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: getInviteCodeReqbody,
		success: function(data) {
			if(data.statusCode == 0) {
				$.toptip("短信验证码已发送到手机！", 'success');
				getInviteCode(val);
			} else {
				$.toptip(data.resultDesc, 'error');
			}
		}
	});
}

function getInviteCode(val) {
	if(countdown == 0) {
		val.removeAttribute("disabled");
		val.value = "验证码";
		countdown = 300;
	} else {
		val.setAttribute("disabled", true);
		val.value = countdown + "秒";
		countdown--;
	}
	setTimeout(function() {
		getInviteCode(val)
	}, 1000);
}
/**
 * 注册页面的逻辑
 */
var first = true;

function Register() {
	//$(".weui-btn").attr("disabled", false);
	if($("#mobile").val().trim() == '') {
		$.toast("手机号码不能为空!", "text");
		return false;
	}
	if($("#password").val().trim() == '') {
		$.toast("密码不能为空!", "text");
		return false;
	}
	if($("#repassword").val().trim() == '') {
		$.toast("密码不能为空!", "text");
		return false;
	}
	if($("#password").val().trim() != $("#repassword").val().trim()) {
		$.toast("两次密码不一致，请检查!", "text");
		return false;
	}
	if($("#yzm").val().trim() == '') {
		$.toast("图片验证码不能为空!", "text");
		return false;
	}
	if($("#validateCode").val().trim() == '') {
		$.toast("短信验证码不能为空!", "text");
		return false;
	}
	if(/^1(3|4|5|6|7|8|9)\d{9}$/.test($("#mobile").val().trim())) {
		number = "+86|" + $("#mobile").val().trim();
	} else {
		number = $("#mobile").val().trim();
	}
	if(/^1(3|4|5|6|7|8|9)\d{9}$/.test($("#inviteCode").val().trim())) {
		number2 = "+86|" + $("#inviteCode").val().trim();
	} else {
		number2 = $("#inviteCode").val().trim();
	}
	var registerReqbody = {
		"type": 0,
		"mobile": number,
		"password": $("#password").val().trim(),
		"repassword": $("#repassword").val().trim(),
		"validateCode": $("#validateCode").val().trim(),
		"inviteCode": number2
	};
	if(first) {
		first = false;
		$.ajax({
			type: "POST",
			url: ip + "platform/user/regist.m",
			xhrFields: {
				withCredentials: true
			},
			crossDomain: true,
			data: registerReqbody,
			success: function(data) {
				if(data.statusCode == 0) {
					//$(".weui-btn").attr("disabled", true);
					$.toptip("注册成功！", 'success');
					setTimeout('window.location.href = "appRegistrationSuccess.html"', 100);
				} else {
					$.toptip(data.resultDesc, 'error');
					first = true;
				}
			}
		});
	}
}

function getinviteUserNameByMobile() {
	var tel = getQueryValue('code');
	$("#inviteCode").val(tel);
	if(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tel)) {
		number = "+86|" + tel;
	} else {
		number = tel;
	}
	var getinviteUserNameByMobileReqbody = {
		"type": 0,
		"mobile": number
	};
	$.ajax({
		type: "POST",
		url: ip + "platform/user/getinviteUserNameByMobile.m",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: getinviteUserNameByMobileReqbody,
		success: function(data) {
			if(data.statusCode == 0) {
				$("#name").text(data.resultDesc);
			} else {
				$.toptip(data.resultDesc, 'error');
			}
		}
	});
}